/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.bank.controller;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.tool.MyStringUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.bank.entity.*;
import org.springblade.modules.bank.service.*;
import org.springblade.modules.bank.util.AESUtil;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.bank.vo.TmVisitTaskCustomerVO;
import org.springblade.core.boot.ctrl.BladeController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static org.springblade.modules.bank.constant.BankConstant.EX1_TYPE_INTRODUCE;

/**
 * 任务客户 控制器
 *
 * @author Blade
 * @since 2022-07-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tmvisittaskcustomer")
@Api(value = "任务客户", tags = "任务客户接口")
@Slf4j
public class TmVisitTaskCustomerController extends BladeController {

	private ITmVisitTaskCustomerService tmVisitTaskCustomerService;

	private ITmVisitTaskService tmVisitTaskService;

	private ITmCustomerCallRecordService tmCustomerCallRecordService;

	private IUserService userService;

	private ITmCustomerTagService tmCustomerTagService;

	private ITmCustomerService tmCustomerService;

	private ITmCheckTimeService tmCheckTimeService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入tmVisitTaskCustomer")
	public R<TmVisitTaskCustomer> detail(TmVisitTaskCustomer tmVisitTaskCustomer) {
		TmVisitTaskCustomer detail = tmVisitTaskCustomerService.getOne(Condition.getQueryWrapper(tmVisitTaskCustomer));
		return R.data(detail);
	}

	/**
	 * 分页 任务客户
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入tmVisitTaskCustomer")
	public R<IPage<TmVisitTaskCustomer>> list(TmVisitTaskCustomer tmVisitTaskCustomer, Query query) {
		IPage<TmVisitTaskCustomer> pages = tmVisitTaskCustomerService.page(Condition.getPage(query), Condition.getQueryWrapper(tmVisitTaskCustomer));
		return R.data(pages);
	}

	/**
	 * 自定义分页 任务客户
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tmVisitTaskCustomer")
	public R<IPage<TmVisitTaskCustomerVO>> page(TmVisitTaskCustomerVO tmVisitTaskCustomer, Query query) {
		IPage<TmVisitTaskCustomerVO> pages = tmVisitTaskCustomerService.selectTmVisitTaskCustomerPage(Condition.getPage(query), tmVisitTaskCustomer);
		return R.data(pages);
	}

	/**
	 * 自定义分页 任务客户
	 */
	@GetMapping("/getVisitCustomerList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tmVisitTaskCustomer")
	public R<IPage<TmVisitTaskCustomerVO>> getVisitCustomerList(TmVisitTaskCustomerVO tmVisitTaskCustomer, Query query) throws Exception {
		tmVisitTaskCustomer.setCustids(new ArrayList<Long>());

		if (tmVisitTaskCustomer.getVisitTime()!=null){
			tmVisitTaskCustomer.setVisitTimeDate(tmVisitTaskCustomer.getVisitTime().toLocalDate());
		}
		if(MyStringUtil.isNotEmpty(tmVisitTaskCustomer.getCustomerName())){
			tmVisitTaskCustomer.setCustomerName(AESUtil.encrypt(tmVisitTaskCustomer.getCustomerName(),BankConstants.KEY));
		}
		if(MyStringUtil.isNotEmpty(tmVisitTaskCustomer.getCustomerTel())){
			tmVisitTaskCustomer.setCustomerTel(AESUtil.encrypt(tmVisitTaskCustomer.getCustomerTel(),BankConstants.KEY));
		}

		IPage<TmVisitTaskCustomerVO> pages = tmVisitTaskCustomerService.getVisitCustomerList(Condition.getPage(query), tmVisitTaskCustomer);
		pages.getRecords().forEach(tmVisitTaskCustomerVO -> {
			try {
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerVO.getCustomerName())){
					tmVisitTaskCustomerVO.setCustomerName(AESUtil.decrypt(tmVisitTaskCustomerVO.getCustomerName(),BankConstants.KEY));
				}
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerVO.getCustomerTel())){
					tmVisitTaskCustomerVO.setCustomerTel(AESUtil.decrypt(tmVisitTaskCustomerVO.getCustomerTel(),BankConstants.KEY));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return R.data(pages);
	}

	/**
	 * 自定义分页 任务客户
	 */
	@GetMapping("/getTaskList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "tmVisitTaskCustomerVO")
	public List<TmVisitTaskCustomerVO> getTaskList(TmVisitTaskCustomerVO tmVisitTaskCustomer) {
		return tmVisitTaskCustomerService.getTaskList(tmVisitTaskCustomer);
	}

	/**
	 * 新增 任务客户
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入tmVisitTaskCustomer")
	public R save(@Valid @RequestBody TmVisitTaskCustomer tmVisitTaskCustomer) {
		boolean save = tmVisitTaskCustomerService.save(tmVisitTaskCustomer);
		// 子表保存成功查询客户数，更新主表客户数
//		if(save){
//			QueryWrapper<TmVisitTaskCustomer> queryWrapper = new QueryWrapper<>();
//			queryWrapper.eq("task_id",tmVisitTaskCustomer.getTaskId());
//			long count = tmVisitTaskCustomerService.count(queryWrapper);
//			UpdateWrapper<TmVisitTask> updateWrapper = new UpdateWrapper<>();
//			updateWrapper.set("cust_number",(count+1));
//			updateWrapper.eq("id",tmVisitTaskCustomer.getTaskId());
//			tmVisitTaskService.update(updateWrapper);
//		}
		return R.status(save);
	}

	/**
	 * 修改 任务客户
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入tmVisitTaskCustomer")
	public R update(@Valid @RequestBody TmVisitTaskCustomer tmVisitTaskCustomer) {
		return R.status(tmVisitTaskCustomerService.updateById(tmVisitTaskCustomer));
	}

	/**
	 * 修改 任务客户
	 */
	@PostMapping("/updateVisit")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入tmVisitTaskCustomer")
	@Transactional(rollbackFor = Exception.class)
	public R updateVisit(@Valid @RequestBody TmVisitTaskCustomerVO tmVisitTaskCustomerParam) {
		LambdaQueryWrapper<TmVisitTaskCustomer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(TmVisitTaskCustomer::getTaskId,tmVisitTaskCustomerParam.getTaskId());
		lambdaQueryWrapper.eq(TmVisitTaskCustomer::getCustMgr,tmVisitTaskCustomerParam.getCustMgr());
		lambdaQueryWrapper.eq(TmVisitTaskCustomer::getCustomerCode,tmVisitTaskCustomerParam.getCustomerCode());
		TmVisitTaskCustomer tmVisitTaskCustomer;
		if (EX1_TYPE_INTRODUCE.equals(tmVisitTaskCustomerParam.getEx2())){
			tmVisitTaskCustomer = tmVisitTaskCustomerService.getById(tmVisitTaskCustomerParam.getId());
		}else{
			tmVisitTaskCustomer = tmVisitTaskCustomerService.getOne(lambdaQueryWrapper);
		}
		if(tmVisitTaskCustomer!=null){
			if ("2".equals(tmVisitTaskCustomerParam.getVisitType())){
				tmVisitTaskCustomer.setVisitStatus(tmVisitTaskCustomerParam.getVisitStatus());
				TmVisitTask byId = tmVisitTaskService.getById(tmVisitTaskCustomer.getTaskId());
				if (byId!=null){
					tmVisitTaskCustomer.setTaskCode(byId.getTaskCode());
					tmVisitTaskCustomer.setDeptId(Long.valueOf(byId.getDeptId()));
					tmVisitTaskCustomer.setDeptName(byId.getDeptName());
				}
				User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getCode, tmVisitTaskCustomer.getCustMgr()));
				tmVisitTaskCustomer.setCustMgrName(user.getRealName());
				tmVisitTaskCustomer.setVisitTime(LocalDateTime.now());
				//预通过
				tmVisitTaskCustomer.setCheckStatus("0");
				tmVisitTaskCustomer.setVisitType(tmVisitTaskCustomerParam.getVisitType());
				tmVisitTaskCustomer.setVisitContent(tmVisitTaskCustomerParam.getVisitContent());
				tmVisitTaskCustomer.setVisitSaveTime(LocalDateTime.now());
				tmVisitTaskCustomer.setVisitOfflineStatus(tmVisitTaskCustomerParam.getVisitOfflineStatus());
				tmVisitTaskCustomer.setVisitOffilneFileSourceId(tmVisitTaskCustomer.getId());
				tmVisitTaskCustomerService.updateById(tmVisitTaskCustomer);

				//更新任务
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerParam.getTaskIds())){
					String[] taskIds = tmVisitTaskCustomerParam.getTaskIds().split(",");
					LambdaQueryWrapper<TmVisitTaskCustomer> otherLQW = new LambdaQueryWrapper<>();
					otherLQW.in(TmVisitTaskCustomer::getTaskId,taskIds);
					otherLQW.eq(TmVisitTaskCustomer::getCustMgr,tmVisitTaskCustomerParam.getCustMgr());
					otherLQW.eq(TmVisitTaskCustomer::getCustomerCode,tmVisitTaskCustomerParam.getCustomerCode());
					List<TmVisitTaskCustomer> otherTaskCustomerList = tmVisitTaskCustomerService.list(otherLQW);
					otherTaskCustomerList.forEach(item->{
						item.setVisitStatus(tmVisitTaskCustomerParam.getVisitStatus());
						item.setCheckStatus(tmVisitTaskCustomer.getCheckStatus());
						item.setCustMgrName(user.getRealName());
						item.setVisitTime(LocalDateTime.now());
						item.setVisitContent(tmVisitTaskCustomerParam.getVisitContent());
						item.setVisitOfflineStatus(tmVisitTaskCustomer.getVisitOfflineStatus());
						item.setVisitOffilneFileSourceId(tmVisitTaskCustomer.getId());
					});
					tmVisitTaskCustomerService.saveOrUpdateBatch(otherTaskCustomerList);
				}
			}else{
				tmVisitTaskCustomer.setHangupStatus(tmVisitTaskCustomerParam.getHangupStatus());
				tmVisitTaskCustomer.setVisitStatus(tmVisitTaskCustomerParam.getVisitStatus());
				TmVisitTask byId = tmVisitTaskService.getById(tmVisitTaskCustomer.getTaskId());
				if (byId!=null){
					tmVisitTaskCustomer.setTaskCode(byId.getTaskCode());
					tmVisitTaskCustomer.setDeptId(Long.valueOf(byId.getDeptId()));
					tmVisitTaskCustomer.setDeptName(byId.getDeptName());
				}
				User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getCode, tmVisitTaskCustomer.getCustMgr()));
				tmVisitTaskCustomer.setCustMgrName(user.getRealName());
				tmVisitTaskCustomer.setVisitTime(LocalDateTime.now());

				TmCustomerCallRecord tmCustomerCallRecord = tmCustomerCallRecordService.getById(tmVisitTaskCustomer.getCallRecordId());
				Duration between = LocalDateTimeUtil.between(tmCustomerCallRecord.getCallRingtimeStart(), tmCustomerCallRecord.getCallRingtimeEnd());
				TmCheckTime tmCheckTime = tmCheckTimeService.getOne(new LambdaQueryWrapper<TmCheckTime>().eq(TmCheckTime::getDeptId,tmVisitTaskCustomer.getDeptId()));
				Integer checkTime = tmCheckTime==null?40:tmCheckTime.getParamValue();
				if ((between.toMillis()/1000)>checkTime){
					//预通过
					tmVisitTaskCustomer.setCheckStatus("0");
				}else{
					//不通过
					tmVisitTaskCustomer.setCheckStatus("-1");
				}

				tmVisitTaskCustomerService.updateById(tmVisitTaskCustomer);

				//更新任务
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerParam.getTaskIds())){
					List<TmCustomerCallRecord> recordList = new ArrayList<>();

					String[] taskIds = tmVisitTaskCustomerParam.getTaskIds().split(",");
					LambdaQueryWrapper<TmVisitTaskCustomer> otherLQW = new LambdaQueryWrapper<>();
					otherLQW.in(TmVisitTaskCustomer::getTaskId,taskIds);
					otherLQW.eq(TmVisitTaskCustomer::getCustomerCode,tmVisitTaskCustomerParam.getCustomerCode());
					List<TmVisitTaskCustomer> otherTaskCustomerList = tmVisitTaskCustomerService.list(otherLQW);
					otherTaskCustomerList.forEach(item->{
						item.setCustMgrName(user.getRealName());
						item.setHangupStatus(tmVisitTaskCustomerParam.getHangupStatus());
						item.setVisitStatus(tmVisitTaskCustomerParam.getVisitStatus());
						item.setVisitTime(LocalDateTime.now());
						item.setCallRecordId(tmCustomerCallRecord.getId());
						item.setCheckStatus(tmVisitTaskCustomer.getCheckStatus());
//					if (tmCustomerCallRecord!=null){
						//因为复制后，文件名会重复，会导致录音上传时，无法定位到相应的记录，因此，不能复制录音记录
//						TmCustomerCallRecord tempRecord = new TmCustomerCallRecord();
//						BeanUtil.copyProperties(tmCustomerCallRecord,tempRecord,"id");
//						tempRecord.setTaskId(item.getTaskId());
//						tempRecord.setTaskCode(item.getTaskCode());
//						tmCustomerCallRecordService.saveOrUpdate(tempRecord);
//						item.setCallRecordId(tempRecord.getId());
//					}
					});
					tmVisitTaskCustomerService.saveOrUpdateBatch(otherTaskCustomerList);
				}
			}
			return R.status(true);
		}else{
			return R.status(false);
		}
	}


	/**
	 * 新增或修改 任务客户
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入tmVisitTaskCustomer")
	public R submit(@Valid @RequestBody TmVisitTaskCustomer tmVisitTaskCustomer) {
		Integer option;
//		QueryWrapper<TmVisitTaskCustomer> queryWrapper = new QueryWrapper<>();
		long cuscount = 0;
		if(null == tmVisitTaskCustomer.getId()){
			option = 1;
			cuscount = tmVisitTaskCustomerService.count(new QueryWrapper<TmVisitTaskCustomer>().lambda()
					.eq(TmVisitTaskCustomer::getTaskId, tmVisitTaskCustomer.getTaskId())
					.eq(TmVisitTaskCustomer::getCustomerCode, tmVisitTaskCustomer.getCustomerCode()));
		}else{
			option = 2;
			tmVisitTaskCustomerService.removeById(tmVisitTaskCustomer.getId());
			cuscount = 0;
		}

		if(cuscount>0){
			return R.fail(tmVisitTaskCustomer.getCustomerCode()+"客户编码已存在");
		}
		boolean save = tmVisitTaskCustomerService.saveOrUpdate(tmVisitTaskCustomer);
		// 子表保存成功查询客户数，更新主表客户数
//		if(save){
//			if(option == 1){
//				QueryWrapper<TmVisitTaskCustomer> queryWrapper = new QueryWrapper<>();
//				queryWrapper.eq("task_id",tmVisitTaskCustomer.getTaskId());
//				long count = tmVisitTaskCustomerService.count(queryWrapper);
//				UpdateWrapper<TmVisitTask> updateWrapper = new UpdateWrapper<>();
//				updateWrapper.set("cust_number",count);
//				updateWrapper.eq("id",tmVisitTaskCustomer.getTaskId());
//				tmVisitTaskService.update(updateWrapper);
//			}
//		}
		return R.status(save);
	}


	/**
	 * 删除 任务客户
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> longs = Func.toLongList(ids);
		TmVisitTaskCustomer byId = tmVisitTaskCustomerService.getById(longs.get(0));
		boolean b = tmVisitTaskCustomerService.removeByIds(longs);
		// 子表删除功查询客户数，更新主表客户数
//		if(b){
//			QueryWrapper<TmVisitTaskCustomer> queryWrapper = new QueryWrapper<>();
//			queryWrapper.eq("task_id",byId.getTaskId());
//			long count = tmVisitTaskCustomerService.count(queryWrapper);
//			UpdateWrapper<TmVisitTask> updateWrapper = new UpdateWrapper<>();
//			updateWrapper.set("cust_number",count);
//			updateWrapper.eq("id",byId.getTaskId());
//			tmVisitTaskService.update(updateWrapper);
//		}
		return R.status(b);
	}

	/**
	 * 新增或修改 任务客户
	 */
	@PostMapping("/taskCheck")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入tmVisitTaskCustomer")
	public R taskCheck(@Valid @RequestBody TmVisitTaskCustomer tmVisitTaskCustomer) {
		BladeUser bladeUser = SecureUtil.getUser();

		TmVisitTaskCustomer task = tmVisitTaskCustomerService.getById(tmVisitTaskCustomer.getId());
		task.setCheckStatus(tmVisitTaskCustomer.getCheckStatus());
		//已核查
		task.setCheckType("1");
		task.setCheckTime(LocalDateTime.now());
		task.setCheckBy(bladeUser.getUserId());
		task.setCheckByName(bladeUser.getUserName());
		tmVisitTaskCustomerService.updateById(task);
		return R.status(true);
	}

	/**
	 * 自定义分页 任务客户
	 */
	@GetMapping("/getTaskCount")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "tmVisitTaskCustomerVO")
	public R getTaskCount(TmVisitTaskCustomerVO tmVisitTaskCustomer) {
		if (tmVisitTaskCustomer.getTaskId()==null||tmVisitTaskCustomer.getTaskId()==0){
			return R.fail("任务统计失败，任务id为空");
		}
		/**
		 * 部门日常任务查询 正常1个月1个日常任务
		 * this.query.ex1='2';
		 * this.query.deptId = this.$store.state.user.baseUser.deptId;
		 * this.query.taskBeginDate = dateFormatForMonth(new Date())+"-01";
		 */
		/**
		 * check_status 0预通过  1是通过  -1是不通过
		 */
		/**
		 * 日常任务
		 * taskId  custMgr
		 * this.query.custMgr = this.$store.state.user.userInfo.code;
		 */

		/**
		 * 日常任务核查
		 * taskId
		 */
		String dailyTaskCountSql = "";
		if (MyStringUtil.isNotEmpty(tmVisitTaskCustomer.getCustMgr())){
			dailyTaskCountSql = "select 'zj' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "'\n" +
					"union all \n" +
					"select 'wwc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and t.visit_status='0'\n" +
					"union all \n" +
					"select 'ywc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and t.visit_status='1'\n" +
					"union all \n" +
					"select 'drwc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and date(t.visit_time)=date(sysdate()) and t.visit_status='1'\n" +
					"union all \n" +
					"select 'ytg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and t.check_status='0'\n" +
					"union all \n" +
					"select 'tg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and t.check_status='1'\n" +
					"union all \n" +
					"select 'wtg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.cust_mgr='" + tmVisitTaskCustomer.getCustMgr() + "' and t.check_status='-1'";
		}else{
			dailyTaskCountSql = "select 'zj' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + "\n" +
					"union all \n" +
					"select 'wwc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.visit_status='0'\n" +
					"union all \n" +
					"select 'ywc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.visit_status='1'\n" +
					"union all \n" +
					"select 'drwc' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and date(t.visit_time)=date(sysdate()) and t.visit_status='1'\n" +
					"union all \n" +
					"select 'ytg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.check_status='0'\n" +
					"union all \n" +
					"select 'tg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.check_status='1'\n" +
					"union all \n" +
					"select 'wtg' label,count(*) ct from tm_visit_task_customer t where t.task_id=" + tmVisitTaskCustomer.getTaskId() + " and t.check_status='-1'";
		}

		List<Map> list = tmCustomerCallRecordService.queryBySql(dailyTaskCountSql);
		Map<String,Object> resultMap = new HashMap();
		list.stream().forEach(item->{
			resultMap.put(item.get("label").toString(),item.get("ct"));
		});
		return R.data(resultMap);
	}

	/**
	 * 自定义分页 任务客户
	 */
	@GetMapping("/getVisitCustomerListByMgr")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tmVisitTaskCustomer")
	public R<IPage<TmVisitTaskCustomerVO>> getVisitCustomerListByMgr(TmVisitTaskCustomerVO tmVisitTaskCustomer, Query query) throws Exception {
		if (tmVisitTaskCustomer.getVisitTime()!=null){
			tmVisitTaskCustomer.setVisitTimeDate(tmVisitTaskCustomer.getVisitTime().toLocalDate());
		}
		if(MyStringUtil.isNotEmpty(tmVisitTaskCustomer.getCustomerTel())){
			tmVisitTaskCustomer.setCustomerTel(AESUtil.encrypt(tmVisitTaskCustomer.getCustomerTel(),BankConstants.KEY));
		}

		IPage<TmVisitTaskCustomerVO> pages = tmVisitTaskCustomerService.getVisitCustomerListByMgr(Condition.getPage(query), tmVisitTaskCustomer);
		pages.getRecords().forEach(tmVisitTaskCustomerVO -> {
			try {
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerVO.getCustomerName())){
					tmVisitTaskCustomerVO.setCustomerName(AESUtil.decrypt(tmVisitTaskCustomerVO.getCustomerName(),BankConstants.KEY));
				}
				if(MyStringUtil.isNotEmpty(tmVisitTaskCustomerVO.getCustomerTel())){
					tmVisitTaskCustomerVO.setCustomerTel(AESUtil.decrypt(tmVisitTaskCustomerVO.getCustomerTel(),BankConstants.KEY));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return R.data(pages);
	}
}
